<template>
	<view class="content">
		<view v-if="list.length > 0" class="user-list">
			<block v-for="(item, index) in list" :key="index">
				<view class="user-item" @click="openUserDetails(item)">
					<view class="user-avatar">
						<image class="avatar" :src="getAvatarUrl(item.avatar)" mode="aspectFill"></image>
					</view>
					<view class="info-wrap">
						<view class="user-info">
							<view class="name">
								<text>{{item.nick}}</text>
							</view>
							<view class="info">
								<view :class="['sex-wrap', 'sex' + getSex(item.gender)]">
									<image class="sex-icon" :src="'/static/img/sex' + getSex(item.gender) + '.png'" mode=""></image>
								</view>
							</view>
						</view>
					</view>
					<view class="uni-icon uni-icon-arrowright"></view>
				</view>
			</block>
		</view>
		<view v-else class="empty-wrap">
			<image class="empty-icon" src="/static/img/empty.png" mode=""></image>
			<text class="empty-text">暂无好友</text>
		</view>
	</view>
</template>

<script>
	import util from "@/common/util.js"
	export default {
		data() {
			return {
				myInfo: {},
				list: [],
			}
		},
		onLoad(options) {
			try {
				const myInfo = uni.getStorageSync('myInfo');
				if (myInfo) {
					this.myInfo = myInfo;
				}
			} catch (e) {
				console.log(e);
			}
		},
		onReady() {
			this.getFriendList();
		},
		onPullDownRefresh() {
			this.page = 1;
			setTimeout(() => {
				this.getFriendList();
			}, 500)
		},
		methods: {
			getFriendList: function () {
				uni.showLoading({
					title: ''
				})
				this.$tim.getFriendList({
					fromAccount: this.IDBEFORE + this.myInfo.id + ''
				}).then(imResponse => {
					console.log('获取好友列表成功:', imResponse)
					let friendList = [];
					for (let i = 0; i < imResponse.data.length; i++) {
						friendList.push(imResponse.data[i].userID)
					}
					if (friendList.length > 0) {
						this.$tim.getUserProfile({
							userIDList: friendList
						}).then(imResponse => {
							uni.hideLoading();
							uni.stopPullDownRefresh();
							console.log('获取用户资料成功:', JSON.stringify(imResponse.data));
							this.list = imResponse.data
						}).catch(imError => {
							uni.hideLoading();
							uni.stopPullDownRefresh();
							console.warn('获取用户资料失败:', imError);
						});
					} else {
						uni.hideLoading();
						uni.stopPullDownRefresh();
					}
				}).catch(imError => {
					uni.hideLoading();
					uni.stopPullDownRefresh();
					console.warn('获取好友列表失败:', imError);
				});
			},
			
			getAvatarUrl: function (value) {
				return value.indexOf(this.QINIUURL) == -1 ? value : value + '?imageView2/1/w/100/h/100'
			},
			
			getSex: function (value) {
				let sex = '1';
				if (value == 'Gender_Type_Female') {
					sex = '2';
				}
				return sex;
			},
			
			openUserDetails: function (item) {
				let user = {
					id: util.getUserID(item.userID, this.IDBEFORE),
					name: item.nick,
					avatar:item.avatar
				}
				uni.navigateTo({
					url: '../home/user_home?user=' + JSON.stringify(user)
				})
			},
		}
	}
</script>

<style lang="scss">
	@import '../../common/common.scss';
	.user-list {
		padding: 1rpx 0;
	}
	.user-item {
		display: flex;
		justify-content: flex-start;
		align-items: center;
		padding: 20rpx;
		background-color: #fff;
		.user-avatar {
			display: flex;
			justify-content: center;
			align-items: center;
			padding: 1rpx;
			border: 4rpx solid #fff;
			border-radius: 120rpx;
			position: relative;
			.avatar {
				width: 100rpx;
				height: 100rpx;
				border-radius: 100rpx;
				background-color: $image-bg-color;
			}
		}
		.info-wrap {
			display: flex;
			justify-content: flex-start;
			align-items: center;
			flex: 1;
			height: 140rpx;
			border-bottom: 1rpx solid #f9f9f9;
		}
		.user-info {
			flex: 1;
			margin: 0 20rpx;
			.name {
				display: flex;
				justify-content: flex-start;
				align-items: center;
				text {
					max-width: 360rpx;
					overflow: hidden;
					white-space: nowrap;
					text-overflow: ellipsis;
					font-size: 36rpx;
					color: #333;
				}
			}
			.info {
				display: flex;
				justify-content: flex-start;
				align-items: center;
			}
			.sex-wrap {
				display: flex;
				justify-content: center;
				align-items: center;
				line-height: 28rpx;
				padding: 5rpx 10rpx;
				border-radius: 28rpx;
				&.sex1 {
					background: #83d7ff;
				}
				&.sex2 {
					background: #fe9ab7;
				}
				.sex-icon {
					width: 28rpx;
					height: 28rpx;
				}
				.age {
					font-size: 28rpx;
					color: #fff;
					margin-left: 5rpx;
				}
			}
			.city-wrap {
				display: flex;
				justify-content: center;
				align-items: center;
				line-height: 28rpx;
				padding: 5rpx 10rpx;
				border-radius: 28rpx;
				background: #4cbcfc;
				margin-left: 10rpx;
				.city-icon {
					width: 28rpx;
					height: 28rpx;
				}
				.city {
					font-size: 28rpx;
					color: #fff;
					margin-left: 5rpx;
				}
			}
			.intro {
				width: 360rpx;
				font-size: 32rpx;
				color: #999999;
				overflow: hidden;
				white-space: nowrap;
				text-overflow: ellipsis;
				margin-top: 10rpx;
			}
		}
		.uni-icon {
			font-size: 28rpx;
			color: #999999;
		}
	}
</style>
